package com.fadadianqi.mall.admin.controller;

import com.fadadianqi.logistics.admin.controller.form.AjaxResult;
import com.fadadianqi.commons.JSONProvider;
import com.fadadianqi.goods.model.Brand;
import com.fadadianqi.goods.model.Category;
import com.fadadianqi.goods.model.Goods;
import com.fadadianqi.goods.service.GoodsService;
import com.fadadianqi.logistics.search.GoodsSearcher;
import com.fadadianqi.search.Result;
import com.yeqiangwei.commons.util.Pagination;
import com.yeqiangwei.commons.util.ParamUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by aroot on 2/10/14.
 */
@Controller
public class GoodsController {

    @RequestMapping(value = "/mall/ajax/item/search")
    public ResponseEntity<String> ajaxSelector(HttpServletRequest request, ModelMap modelMap) {
        Pagination pagination = new Pagination(1, 100);
        Result result = null;
        try {
            result = goodsSearcher.search(ParamUtils.getString(request, "q", null), pagination);
        } catch (Exception e) {
            e.printStackTrace();
        }
        List<GoodsView> command = new ArrayList<GoodsView>();
        if (null!=result && result.getItems() != null && result.getItems().size() > 0) {
            List<?> list = result.getItems();
            for (Object o : list) {
                GoodsSearcher.GoodsVO vo = (GoodsSearcher.GoodsVO) o;
                Goods goods = goodsService.getGoodsById(vo.getId());
                Category category = goodsService.getCategoryById(vo.getCategoryId());
                Brand brand = goodsService.getBrandById(vo.getBrandId());
                GoodsView gv = new GoodsView();
                gv.setId(goods.getId());
                gv.setBrandId(goods.getBrandId());
                gv.setCategoryId(goods.getCategoryId());
                gv.setBrandName(brand.getName());
                gv.setCategoryName(category.getName());
                gv.setName(goods.getName());
                gv.setDisplayName(goods.getDisplayName());
                gv.setBarcode(goods.getBarcode());
                command.add(gv);
            }
        }
        HttpHeaders responseHeaders = new HttpHeaders();
        responseHeaders.add("Content-Type", "text/html; charset=utf-8");
        AjaxResult rs = new AjaxResult(command, Boolean.TRUE, null);
        return new ResponseEntity<String>(JSONProvider.toJSON(rs),
                responseHeaders, HttpStatus.OK);
    }
    @Autowired
    private GoodsService goodsService;

    @Autowired
    private GoodsSearcher goodsSearcher;

    public static class GoodsView implements Serializable {

        private static final long serialVersionUID = -7777;

        private String id;
        private String name;
        private String displayName;
        private String barcode;
        private String  categoryId;
        private String  categoryName;
        private String brandId;
        private String brandName;

        public String getDisplayName() {
            return displayName;
        }

        public void setDisplayName(String displayName) {
            this.displayName = displayName;
        }

        public String getId() {
            return id;
        }

        public void setId(String id) {
            this.id = id;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public String getBarcode() {
            return barcode;
        }

        public void setBarcode(String barcode) {
            this.barcode = barcode;
        }

        public String getCategoryId() {
            return categoryId;
        }

        public void setCategoryId(String categoryId) {
            this.categoryId = categoryId;
        }

        public String getCategoryName() {
            return categoryName;
        }

        public void setCategoryName(String categoryName) {
            this.categoryName = categoryName;
        }

        public String getBrandId() {
            return brandId;
        }

        public void setBrandId(String brandId) {
            this.brandId = brandId;
        }

        public String getBrandName() {
            return brandName;
        }

        public void setBrandName(String brandName) {
            this.brandName = brandName;
        }
    }
}
